package cn.monkey.family.server.domain.uc.role;

import cn.monkey.family.data.Result;
import cn.monkey.family.data.context.Context;
import cn.monkey.family.data.context.RequestContext;
import cn.monkey.family.data.uc.role.RoleDto;
import cn.monkey.family.data.uc.role.RoleQueryRequest;
import cn.monkey.family.data.uc.role.RoleVo;
import com.google.common.base.Strings;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("role")
public class RoleController {
    private final IRoleService roleService;

    public RoleController(IRoleService roleService) {
        this.roleService = roleService;
    }

    @GetMapping({"{id}", ""})
    Result<?> get(@Context RequestContext requestContext,
                  @PathVariable(value = "id", required = false) String id,
                  RoleQueryRequest queryRequest,
                  Pageable pageable) {
        if (!Strings.isNullOrEmpty(id)) {
            return this.roleService.readById(requestContext, id);
        }
        if (pageable.isUnpaged()) {
            return this.roleService.readAll(requestContext, queryRequest, pageable.getSort());
        }
        return this.roleService.readPage(requestContext, queryRequest, pageable);
    }


    @PostMapping
    Result<RoleVo> create(@Context RequestContext requestContext,
                          @RequestBody RoleDto roleDto) {
        return roleService.create(requestContext, roleDto);
    }

    @PutMapping("{id}")
    Result<RoleVo> update(@Context RequestContext requestContext,
                          @PathVariable("id") String id,
                          @RequestBody RoleDto roleDto) {
        return this.roleService.update(requestContext, id, roleDto);
    }

    @DeleteMapping("{id}")
    Result<Void> delete(@Context RequestContext requestContext,
                        @PathVariable("id") String id) {
        return this.roleService.delete(requestContext, id);
    }
}
